package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import util.ConnectionManager;
import util.Utilities;


public class UserBean {
    
    public final static String ADMIN_IDEF = "admin";
    
    public final static String USER_IDEF = "customer";
    
    public final static String STOCK_IDEF = "stockAdmin";
    
    private String userID;
    
    private String firstname;
    
    private String lastname;
    
    private int age;
    
    private String gender;
    
    private String interest;
    
    private String address;
    
    private String country;
    
    private int zipcode;
    
    private String password;
    
    private String identity;
    
    private String email;
    
    public UserBean() {
    }
    
    public UserBean(String userID, String firstname, String lastname, int age, String gender, String interest, String address,
            String country, int zipcode, String password, String identity, String email) {
        this.userID = userID;
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;
        this.gender = gender;
        this.interest = interest;
        this.address = address;
        this.country = country;
        this.zipcode = zipcode;
        this.password = password;
        this.identity = identity;
        this.email = email;
    }
    
    public static UserBean[] getAllUser() {
        return User("select * from member");
    }
    public static UserBean[] User(String query) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        UserBean[] finalResult = null;
        try {
            con = ConnectionManager.connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            ArrayList<UserBean> result = new ArrayList<UserBean>();
            while(rs.next()) {
                UserBean user = oneColumnOfResultSetToUser(rs);
                result.add(user);
            }
            finalResult = result.toArray(finalResult);
        }catch (Exception e) {
            
        }finally {
               Utilities.close(rs, stmt, con);
        }
        return finalResult;
    }
    
    private static UserBean oneColumnOfResultSetToUser(final ResultSet rs) throws SQLException {
        String userID = rs.getString("userID");
        String firstname = rs.getString("firstname");
        String lastname = rs.getString("lastname");
        int age = rs.getInt("age");
        String gender = rs.getString("gender");
        String interest = rs.getString("interest");
        String address = rs.getString("address");
        String country = rs.getString("country");
        int zipcode = rs.getInt("zipcode");
        String password = rs.getString("password");
        String identity = rs.getString("identity");
        String email = rs.getString("email");
        UserBean user = new UserBean(userID, firstname, lastname, age, gender, interest, address, country, zipcode,
                password, identity, email);
        return user;
    }
    
    public static UserBean[] getUserByUserId(String Id) {
        return User("select * from member where userID='"+Id+"'");
    }
    
    public static boolean isIDExist(String Id) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        boolean result = false;
        try {
            con = ConnectionManager.connect();
            String query = "select userID from member where userID='"+Id+"'";
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            result = rs.next();
        }catch (Exception e) {
        }
        return result;
    }
    public static boolean createUser(String userID, String firstname, String lastname, String age1, String gender,
            String interest, String address, String country, String zipcode, String password, String email) {
        Connection con = null;
        PreparedStatement update = null;
        ResultSet rs = null;
        boolean isSuccess = false;
        try {
            int age = Integer.parseInt(age1);
            con = ConnectionManager.connect();
            con.setAutoCommit(false);
            String sql ="INSERT INTO member (userID, firstname, lastname, age, gender," +
                    " interest, address, country, zipcode, password, identity, e-mail)" +
                    " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            update = con.prepareStatement(sql);
            
            update.setString(1, userID);
            update.setString(2, firstname);
            update.setString(3, lastname);
            update.setInt(4, age);
            update.setString(5, gender);
            update.setString(6, interest);
            update.setString(7, address);
            update.setString(8, country);
            update.setString(9, zipcode);
            update.setString(10, password);
            update.setString(11, beans.UserBean.USER_IDEF);
            update.setString(12, email);
            update.execute();
            con.commit();
            isSuccess = true;
        }catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isSuccess;
    }
    
    public static String login(String username1, String password1) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        String identity = null;
        try {
            String query = "select * from member where userID ='" + username1 + "' AND password ='" + password1 + "'";
            con = ConnectionManager.connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            if(rs.next())
                identity = rs.getString("identity");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return identity;
        
    }
    
    public static boolean update(String userID, String firstname, String lastname, int age, String gender,
            String interest, String address, String country, String zipcode, String password, String email) {
        Connection con = null;
        PreparedStatement update = null;
        ResultSet rs = null;
        boolean isSuccess = false;
        try {
            con = ConnectionManager.connect();
            con.setAutoCommit(false);
            String sql = "UPDATE member SET firstname=?, lastname=?, age=?, gender=?, interest=?, " +
                    "address=?, country=?, zipcode=?, password=?, identity=?, e-mail=? where userID = ?";
            update = con.prepareStatement(sql);            
            
            update.setString(1, firstname);
            update.setString(2, lastname);
            update.setInt(3, age);
            update.setString(4, gender);
            update.setString(5, interest);
            update.setString(6, address);
            update.setString(7, country);
            update.setString(8, zipcode);
            update.setString(9, password);
            update.setString(10, beans.UserBean.USER_IDEF);
            update.setString(11, email);
            update.setString(12, userID);
            update.execute();
            con.commit();
            isSuccess = true;
        }catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isSuccess;
    }
    
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getUserID() {
        return userID;
    }
    
    public String getFirstname() {
        return firstname;
    }
    
    public String getLastname() {
        return lastname;
    }
    
    public int getAge() {
        return age;
    }
    
    public String getGender() {
        return gender;
    }
    
    public String getInterest() {
        return interest;
    }
    
    public String getAddress() {
        return address;
    }
    
    public String getCountry() {
        return country;
    }
    
    public int getZipcode() {
        return zipcode;
    }
    
    public String getPassword() {
        return password;
    }
    
    public String getIdentity() {
        return identity;
    }
}
